// Package logging
// Title 			zap.logging.components.go
// Description 		日志处理组件
// Author  			alittlexincan@163.com 					2024/07/22 11:36
// Update  			alittlexincan@163.com 					2024/07/22 11:36
package logging

import (
	"context"

	"go.opentelemetry.io/otel"
	"go.uber.org/zap"
)

// TraceLogger
// Title    			TraceLogger
// Description 			自定义日志包装器
// Author  				alittlexincan@163.com 					2024/07/22 11:36
// Update  				alittlexincan@163.com 					2024/07/22 11:36
type TraceLogger struct {

	// Logger 日志实例
	*zap.Logger
}

// ContextLogger
// Title    			ContextLogger
// Description   		从Context中获取TraceID
// Author      			alittlexincan@163.com       			2024/07/22 11:36
// Update    			alittlexincan@163.com 					2024/07/22 11:36
// Param    			ctx context.Context						上下文实例
// Return    			*zap.Logger     						日志实例
func (log *TraceLogger) ContextLogger(ctx context.Context) *zap.Logger {
	tracer := otel.GetTracerProvider().Tracer("middleware")
	// 创建新的Span
	ctx, span := tracer.Start(ctx, "request-handler")
	defer span.End()
	if spanCtx := span.SpanContext(); spanCtx.IsValid() {
		return log.Logger.With(zap.String("traceId", spanCtx.TraceID().String()), zap.String("spanId", spanCtx.SpanID().String()))
	}
	return log.Logger
}
